#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
    umask(0);

    int fd = open("log.txt", O_WRONLY | O_CREAT, 0664);
    if (fd < 0)
    {
        perror("file open error");
    }
    else
    {
        printf("fd = %d\n", fd);
        const char* str = "hello world!";
        write(fd, str, strlen(str));
        close(fd);
    }

    fd = open("log.txt", O_RDONLY);
    if (fd < 0)
    {
        perror("file open error");
    }
    else
    {
        printf("fd = %d\n", fd);
        char buffer[1024];
        int n = read(fd, buffer, sizeof(buffer));
        buffer[n] = '\0';
        printf("read: %s\n", buffer);
        close(fd);
    }

    return 0;
}
